<?php
namespace Ygcl\Service;
use Think\Model;
class UsersService extends Model{
	
	private $m;
	
	function __construct(){
		$this->m = D('Users');
	}
	
	//获取老师和校区的关系表
	public function _get_by_did($did,$keyword,$sortkey,$sortkeyType,$cur=1,$pagesize=10){
		if(!$cur) $cur = 1;
		if(!$pagesize) $pagesize = 20;
	
		$sql = 'SELECT DISTINCT * FROM mini_users A JOIN mini_users_district_relation B WHERE A.uid=B.uid';
		$sqlcount = 'SELECT count(DISTINCT A.uid) as tp_count FROM mini_users A JOIN mini_users_district_relation B WHERE A.uid=B.uid';
		
		if($did){
			$sql .= ' AND B.did = '.$did;
			$sqlcount .= ' AND B.did = '.$did;
		}
		
		if($keyword){
			$sql .= ' AND A.realname like "%'.$keyword.'%"';
			$sqlcount .= ' AND A.realname like "%'.$keyword.'%"';
		}
		if($sortkey){
			if(!$sortkeyType) $sortkeyType = 'DESC';
			$sql .= ' ORDER BY A.`'.$sortkey.'` '.$sortkeyType ;
		}
	
		$sql .= ' limit '.($cur-1)*$pagesize.','.$pagesize;
		$Model = new Model();
		$count = $Model->query($sqlcount);
		$result = array(
				'data' => $Model->query($sql),
				'total' => $count[0]['tp_count'],
				'pages' => ceil($count[0]['tp_count']/$pagesize),
		);
		return $result;
	}
	
   
}